/**
 * plotModule.js剧情以及引导处理模块
 */


let utils = {autoJSUtil: null, gestureUtil: null}

function loadUtils() {
    const configStorage = storages.create('config')
    utils.autoJSUtil = eval(configStorage.get('autoJSUtil'))
    utils.gestureUtil = eval(configStorage.get('gestureUtil'))
    utils.imageHandleUtil = eval(configStorage.get('imageHandleUtil'))
    utils.fileUtil = eval(configStorage.get('fileUtil'))
}


function main(){
    console.log('开始剧情引导模块')
    // toastLog('开始剧情引导模块')
    loadUtils()

    // console.log('设备分辨率:' + device.width + ', ' + device.height)

    // 查看当前是否在弹出窗口中需要关闭弹出遮罩窗口
    let captureimg = captureScreen()
    const courseDataStorage = storages.create('coursedata')
    let ismask = true
    let findheight = device.height * 0.8
    let findX = Math.floor(device.width * 0.975)
    for(let findindexX = 0;findindexX < 5;findindexX = findindexX + 2){
        for(let findindex = Math.floor(device.height * 0.1); findindex < findheight; findindex = findindex + 3){
            let getcolor = utils.imageHandleUtil.extractARGB(captureimg.pixel(findX + findindexX, findindex))
            
            if((getcolor.r + getcolor.g + getcolor.b) / 3 > 120){
                console.error('不是遮罩层，颜色码：#' + JSON.stringify(getcolor))
                console.error('颜色码坐标：' + findX + ', ' + findindex)
                ismask =  false
                break
            }
        }

        if(ismask == false){ break }
    }
    let ismaskstorage = courseDataStorage.get('ismask')
    if(ismask == true){
        console.error('ismaskstorage-before:' + ismaskstorage)
        let ismaskflag =  ismaskstorage === undefined ? 1 : ismaskstorage + 1
        // if(ismaskstorage == undefined){
        //     ismaskflag = 1
        // }else{
        //     ismaskflag = ismaskstorage + 1
        // }

        console.error('ismaskstorage:' + ismaskstorage)
        console.error('ismaskflag:' + ismaskflag)

        if(ismaskflag > 3){
            click(1, device.height * 0.3)
            courseDataStorage.put('ismask', 0)
            return
        }else{
            courseDataStorage.put('ismask', ismaskflag)
            courseDataStorage.put('isback', 0)
        }

    }else{
        courseDataStorage.put('ismask', 0)
    }

    if(utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/skip.png', 0.9, [device.width * 0.8, 0, device.width * 0.2 - 1, device.height * 0.2]) &&
    utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/skip1.png', 0.9, [device.width * 0.8, 0, device.width * 0.2 - 1, device.height * 0.2])){     // 跳过剧情提示
        let skippoint = utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/skip.png', 0.9, [device.width * 0.8, 0, device.width * 0.2 - 1, device.height * 0.2])
        let skipecolor = images.detectsColor(captureimg, '#B6DBF9', skippoint.x + 1, skippoint.y + 1, 16)
        if(skippoint && skipecolor){
            toastLog('找到skip')
            console.error('找到skip')
            click(skippoint.x - 2, skippoint.y + 5)
            sleep(500)

            // 检测是否有剧情跳过确认提示框
            let skipsuerpoint = utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/skipsuer.png', 0.9, [device.width * 0.3, device.height * 0.2, device.width * 0.4, device.height * 0.5])
            let suerbuttonpoint = utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/suerbutton.png', 0.9, [device.width * 0.3, device.height * 0.3, device.width * 0.4, device.height * 0.5])
            if(skipsuerpoint && suerbuttonpoint){
                toastLog('找到跳过剧情询问窗口')
                console.error('找到跳过剧情询问窗口')
                click(suerbuttonpoint.x + 10, suerbuttonpoint.y + 30)
                sleep(500)
            }else if(suerbuttonpoint){
                toastLog('没找到跳过剧情询问窗口')
                console.error('没找到跳过剧情询问窗口')
                sleep(500)
                click(suerbuttonpoint.x - device.width * 0.15, suerbuttonpoint.y + 30)
                sleep(500)
            }

            courseDataStorage.put('ismask', 0)
            courseDataStorage.put('isback', 0)
            return
        }
    }else if(utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/continuegame.png', 0.9, [device.width * 0.7, device.height * 0.8, device.width * 0.3 - 1, device.height * 0.2 - 1]) &&
        utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/continuegame2.png', 0.9, [device.width * 0.7, device.height * 0.8, device.width * 0.3 - 1, device.height * 0.2 - 1])){
        let skippoint = utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/continuegame2.png', 0.9, [device.width * 0.7, device.height * 0.8, device.width * 0.3 - 1, device.height * 0.2 - 1])
        if(skippoint){
            toastLog('找到继续游戏')
            console.error('找到继续游戏')
            click(skippoint.x + 50, skippoint.y - 20)
            sleep(500)
            courseDataStorage.put('ismask', 0)
            courseDataStorage.put('isback', 0)
            return
        }
    }else if(utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/treasurechest.png', 0.9, [device.width * 0.2, device.height * 0.2, device.width * 0.6, device.height * 0.4]) ||
        utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/treasurechest2.png', 0.9, [device.width * 0.2, device.height * 0.2, device.width * 0.6, device.height * 0.4])){
        let cancelpoint = utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/cancel.png', 0.9, [device.width * 0.2, device.height * 0.3, device.width * 0.4, device.height * 0.5])
        if(cancelpoint){
            toastLog('找到回溯对话框')
            console.error('找到回溯对话框')
            click(cancelpoint.x + 10, cancelpoint.y + 30)
            sleep(500)

            courseDataStorage.put('ismask', 0)
            courseDataStorage.put('isback', 0)
            return
        }
    }else if(utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/downloadaudio.png', 0.9, [device.width * 0.2, device.height * 0.2, device.width * 0.6, device.height * 0.4])){      // 当前存在不能跳过对话框，需要对话
        let cancelpoint = utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/suerbutton.png', 0.9, [device.width * 0.2, device.height * 0.3, device.width * 0.4, device.height * 0.5])
        if(cancelpoint){
            toastLog('找到下载语音包对话框')
            console.error('找到下载语音包对话框')
            click(cancelpoint.x + 10, cancelpoint.y + 30)
            sleep(1000)
            let suerbuttonpoint = utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/suerbutton.png', 0.9, [device.width * 0.3, device.height * 0.3, device.width * 0.4, device.height * 0.5])
            if(suerbuttonpoint){
                toastLog('找到最小化按钮')
                console.error('找到最小化按钮')
                click(suerbuttonpoint.x + 10, suerbuttonpoint.y + 30)
                sleep(500)
            }

            courseDataStorage.put('ismask', 0)
            courseDataStorage.put('isback', 0)
            return
        }
    }else if(utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/messagebox.png', 0.9, [device.width * 0.1, device.height * 0.7, device.width * 0.8, device.height * 0.3 - 1]) &&
        utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/messagebox2.png', 0.9, [device.width * 0.1, device.height * 0.6, device.width * 0.8, device.height * 0.3 - 1])){      // 当前存在不能跳过对话框，需要对话
        let skippoint = utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/messagebox.png', 0.95, [device.width * 0.1, device.height * 0.7, device.width * 0.8, device.height * 0.3 - 1])
        if(skippoint  && images.detectsColor(captureimg, '#E3DBCA', skippoint.x + 1, skippoint.y + 1, 4)){
            toastLog('找到不能跳过对话框')
            console.error('找到不能跳过对话框')
            click(skippoint.x + 65, skippoint.y - 10)
            sleep(500)
            
            courseDataStorage.put('ismask', 0)
            courseDataStorage.put('isback', 0)
            return
        }
    }else if(utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/prizedraw.png', 0.9, [device.width * 0.4, device.height * 0.6, device.width * 0.2, device.height * 0.3 - 1])){      // 当前存在不能跳过对话框，需要对话
        let skippoint = utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/prizedraw.png', 0.9, [device.width * 0.4, device.height * 0.6, device.width * 0.2, device.height * 0.3 - 1])
        if(skippoint){
            toastLog('找到抽扭蛋界面')
            console.error('找到抽扭蛋界面')
            swipe(1, device.height * 0.8, 1, device.height * 0.3, 500)
            sleep(500)
            
            courseDataStorage.put('ismask', 0)
            courseDataStorage.put('isback', 0)
            return
        }
    }else if(utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/forcedcombat.png', 0.9)){      // 点击手指↖指引位置
        let skippoint = utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/forcedcombat.png', 0.9)
        if(skippoint){
            toastLog('强制上阵')
            console.error('强制上阵')
            sleep(500)
            
            let swiptipspoint = utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/swiptips.png', 0.9)
            if(skippoint){
                swipe(skippoint.x + 5, skippoint.y + 5, swiptipspoint.x, swiptipspoint.y + 2, 300)
            }

            courseDataStorage.put('ismask', 0)
            courseDataStorage.put('isback', 0)
            return
        }
    }
    else if(utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/finger_leftup.png', 0.9)){      // 点击手指↖指引位置
        let skippoint = utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/finger_leftup.png', 0.9)
        if(skippoint){
            toastLog('找到↖')
            console.error('找到↖')
            click(skippoint.x - 65, skippoint.y - 60)
            sleep(500)
            
            courseDataStorage.put('ismask', 0)
            courseDataStorage.put('isback', 0)
            return
        }
    }else if(utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/finger_rightup.png', 0.9)){      // 点击手指↗指引位置
        let skippoint = utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/finger_rightup.png', 0.9)
        if(skippoint){
            toastLog('找到↗')
            console.error('找到↗')
            click(skippoint.x + 65 + 40, skippoint.y - 60)
            sleep(500)
            
            courseDataStorage.put('ismask', 0)
            courseDataStorage.put('isback', 0)
            return
        }
    }else if(utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/finger_leftdown.png', 0.9)){      // 点击手指↙指引位置
        let skippoint = utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/finger_leftdown.png', 0.9)
        if(skippoint){
            toastLog('找到↙')
            console.error('找到↙')
            click(skippoint.x - 65, skippoint.y + 60 + 30)
            sleep(500)
            
            courseDataStorage.put('ismask', 0)
            courseDataStorage.put('isback', 0)
            return
        }
    }else if(utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/finger_rightdown.png', 0.9)){      // 点击手指↘指引位置
        let skippoint = utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/finger_rightdown.png', 0.9)
        if(skippoint){
            toastLog('找到↘')
            console.error('找到↘')
            click(skippoint.x + 65 + 40, skippoint.y + 60 + 30)
            sleep(500)
            
            courseDataStorage.put('ismask', 0)
            courseDataStorage.put('isback', 0)
            return
        }
    }else if(utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/close1.png', 0.9, [device.width * 0.6, 0, device.width * 0.4 - 1, device.height * 0.4])){      // 点击手指↘指引位置
        let skippoint = utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/close1.png', 0.9, [device.width * 0.6, 0, device.width * 0.4 - 1, device.height * 0.4])
        if(skippoint){
            toastLog('找到圆圈叉叉')
            console.error('找到圆圈叉叉')
            click(skippoint.x + 5, skippoint.y + 5)
            sleep(500)
            
            courseDataStorage.put('ismask', 0)
            courseDataStorage.put('isback', 0)
            return
        }
    }else if(utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/close2.png', 0.9, [device.width * 0.6, 0, device.width * 0.4 - 1, device.height * 0.4])){      // 点击手指↘指引位置
        let skippoint = utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/close2.png', 0.9, [device.width * 0.6, 0, device.width * 0.4 - 1, device.height * 0.4])
        if(skippoint){
            toastLog('找到方框叉叉')
            console.error('找到方框叉叉')
            click(skippoint.x + 5, skippoint.y + 5)
            sleep(500)
            
            courseDataStorage.put('ismask', 0)
            courseDataStorage.put('isback', 0)
            return
        }
    }
    else if(utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/backbutton.png', 0.90, [0, 0, device.width * 0.2, device.height * 0.25])){      // 点击剧情完了后的返回主界面按钮
        let backpoint = utils.imageHandleUtil.findImageInCapture(captureimg, 'plot/backbutton.png', 0.90, [0, 0, device.width * 0.2, device.height * 0.25])
        if(backpoint && images.detectsColor(captureimg, '#F7F9F7', backpoint.x + 1, backpoint.y + 1, 0)){
            let isbackstorage = courseDataStorage.get('isback')
            let isbackflag =  isbackstorage === undefined ? 1 : isbackstorage + 1
            
            if(isbackflag > 4){
                toastLog('找到返回按钮')
                console.error('找到返回按钮')
                click(backpoint.x, backpoint.y + 5)
                courseDataStorage.put('isback', 0)
                return
            }else{
                courseDataStorage.put('ismask', 0)
                courseDataStorage.put('isback', isbackflag)
            }
            sleep(500)
            return
        }else{
            courseDataStorage.put('isback', 0)
        }
    }
}

try {
    main()
} catch (error) {
    console.error(error)
}
